package com.china08.yxyapi.service.yxyresource.impl;

import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.china08.yxyapi.model.resource.SearchResourceModel;
import com.china08.yxyapi.service.yxyresource.YxyResourceBaseService;
import com.china08.yxyapi.service.yxyresource.YxyResourceEduService;

@Service
public class YxyResourceEduServiceImpl implements YxyResourceEduService {

    @Autowired
    private YxyResourceBaseService yxyResourceBaseService;

    @Override
    public long notReviewedNum(SearchResourceModel searchResourceModel) {
        BoolQueryBuilder bool = new BoolQueryBuilder();
        String area = searchResourceModel.area;
        String phaseId = searchResourceModel.phaseId;
        String subjectId = searchResourceModel.subjectId;
        String keyword = searchResourceModel.keyword;
        Integer status = 0;
        Integer viewScope = 2;
        Integer isYxYunRes = 0;
        bool.must(new TermQueryBuilder("isYxYunRes", isYxYunRes));
        bool.must(new TermQueryBuilder("viewScope", viewScope));
        bool.must(new TermQueryBuilder("status", status));
        bool.mustNot(new TermQueryBuilder("typeId", "yx14"));
        bool.must(new TermQueryBuilder("isDelete", 0));

        // 区域
        BoolQueryBuilder areBuilder = new BoolQueryBuilder();
        areBuilder.should(new TermQueryBuilder("provinceCode", area));
        areBuilder.should(new TermQueryBuilder("cityCode", area));
        areBuilder.should(new TermQueryBuilder("districtCode", area));
        bool.must(areBuilder);

        // 学段
        if (StringUtils.isNotBlank(phaseId) && !StringUtils.equals("all", phaseId)) {
            bool.must(new TermQueryBuilder("phaseIdList", phaseId));
        }
        // 学科
        if (StringUtils.isNotBlank(subjectId) && !StringUtils.equals("all", subjectId)) {
            bool.must(new TermQueryBuilder("subjectIdList", subjectId));
        }

        BoolQueryBuilder b1 = new BoolQueryBuilder();
        b1.must(bool);
        long count = 0;
        if (StringUtils.isNotBlank(keyword)) {
            yxyResourceBaseService.keywordTUS(keyword, 1, b1);
            count = yxyResourceBaseService.countResource(b1);
            if (count == 0) {
                BoolQueryBuilder b2 = new BoolQueryBuilder();
                b2.must(bool);
                yxyResourceBaseService.keywordTUS(keyword, 2, b2);
                count = yxyResourceBaseService.countResource(b2);
            }
            if (count == 0) {
                BoolQueryBuilder b4 = new BoolQueryBuilder();
                b4.must(bool);
                yxyResourceBaseService.keywordTUS(keyword, 4, b4);
                count = yxyResourceBaseService.countResource(b4);
            }
            if (count == 0) {
                BoolQueryBuilder b3 = new BoolQueryBuilder();
                b3.must(bool);
                yxyResourceBaseService.keywordTUS(keyword, 3, b3);
                count = yxyResourceBaseService.countResource(b3);
            }
        } else {
            count = yxyResourceBaseService.countResource(b1);
        }
        return count;
    }

}
